﻿using System;
using System.Windows.Forms;

namespace DisplayDemo
{
    public partial class Main : Form
    {
        private Trend t;
        private const string URL = "http://dev.kkcai.net/t.html";

        public Main()
        {
            InitializeComponent();
        }

        private string GetCfg()
        {
            string cfg = String.Empty;

            cfg += rbtnArea1.Checked ? "1" : "0";
            
            if (rbtnColor0.Checked)
                cfg += "0";
            else if (rbtnColor1.Checked)
                cfg += "1";
            else
                cfg += "2";

            if (rbtnFont0.Checked)
                cfg += "0";
            else if (rbtnFont1.Checked)
                cfg += "1";
            else
                cfg += "2";

            cfg += chkDS.Checked ? "1" : "0";
            cfg += chkDX.Checked ? "1" : "0";
            cfg += chkHZ.Checked ? "1" : "0";
            cfg += chkCH.Checked ? "1" : "0";
            cfg += chkLM.Checked ? "1" : "0";
            cfg += chkOmit.Checked ? "1" : "0";
            cfg += chkRowLine.Checked ? "1" : "0";
            cfg += chkTime.Checked ? "1" : "0";
            cfg += chkBorder.Checked ? "1" : "0";

            return cfg;
        }

        #region 进入首页

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("http://www.kkcai.net");
            }
            catch { }
        }

        #endregion

        #region 通知栏

        private void goMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("http://www.kkcai.net");
            }
            catch { }
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出电子走势图控制台吗？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }
        #endregion

        private void Main_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            ReadConfig();
            
            if (new Manager().Valid())
            {
                return;
            }

            notifyIcon1.Visible = false;
            
            new Login().ShowDialog();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!notifyIcon1.Visible)
                notifyIcon1.Visible = true;

            btnExit.Enabled = true;
            btnRefresh.Enabled = true;
            btnStart.Enabled = false;
       
            try
            {
                new Manager().WriteConfig(GetCfg());

                if (t == null)
                    t = new Trend();
                
                t.Show();
                t.webBrowser1.Navigate(String.Format("{0}?{1}", URL, this.GetCfg()));
            }
            catch 
            {
                btnStart.Enabled = true;
                btnExit.Enabled = false;
                btnRefresh.Enabled = false;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (t != null)
            {
                t.Close();
                t = null;

                btnExit.Enabled = false;
                btnStart.Enabled = true;
                btnRefresh.Enabled = false;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            new Manager().WriteConfig(GetCfg());

            if (t != null)
                t.webBrowser1.Navigate(String.Format("{0}?{1}", URL, this.GetCfg()));
        }

        #region 读配置文件

        private void ReadConfig()
        {
            string value = new Manager().ReadConfig();

            switch (value.Substring(0, 1))
            {
                case "0": rbtnArea0.Checked = true; break;
                case "1": rbtnArea1.Checked = true; break;
                default:
                    break;
            }
            switch (value.Substring(1, 1))
            {
                case "0": rbtnColor0.Checked = true; break;
                case "1": rbtnColor1.Checked = true; break;
                case "2": rbtnColor2.Checked = true; break;
                default:
                    break;
            }
            switch (value.Substring(2, 1))
            {
                case "0": rbtnColor0.Checked = true; break;
                case "1": rbtnColor1.Checked = true; break;
                case "2": rbtnColor2.Checked = true; break;
                default:
                    break;
            }
            chkDS.Checked = value.Substring(3, 1).Equals("1");
            chkDX.Checked = value.Substring(4, 1).Equals("1");
            chkHZ.Checked = value.Substring(5, 1).Equals("1");
            chkCH.Checked = value.Substring(6, 1).Equals("1");
            chkLM.Checked = value.Substring(7, 1).Equals("1");
            chkOmit.Checked = value.Substring(8, 1).Equals("1");
            chkRowLine.Checked = value.Substring(9, 1).Equals("1");
            chkTime.Checked = value.Substring(10, 1).Equals("1");
            chkBorder.Checked = value.Substring(11, 1).Equals("1");
        }
        #endregion

        #region 旋转屏幕

        private void btnLeft_Click(object sender, EventArgs e)
        {
            new Manager().ChangeScreen("left");
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            new Manager().ChangeScreen("right");
        }

        #endregion

    }
}
